package cn.hellohao.controller;

import cn.hellohao.pojo.BaiduAiConfig;
import cn.hellohao.utils.JSONChange;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.imageclassify.AipImageClassify;
import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.speech.AipSpeech;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
public class BaiduAiController {
    @Autowired
    private BaiduAiConfig baiduAiConfig;

    /**
     * A.文字识别功能
     * @param file
     * @return
     * @throws Exception
     */
    //001.通用文字识别
    @PostMapping("/ocr")
    public Map ocr(MultipartFile file) throws Exception {
        byte[] buf = file.getBytes();
        AipOcr client = new AipOcr(baiduAiConfig.getAPP_ID(),baiduAiConfig.getAPI_KEY(),baiduAiConfig.getSECRET_KEY());

        HashMap<String, String> options = new HashMap<>();
        options.put("language_type", "CHN_ENG");
        options.put("detect_direction", "true");
        options.put("detect_language", "true");
        options.put("probability", "true");

//        String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1568617533562&di=bc06a8eaee81128a2ed28c52ecb143a4&imgtype=0&src=http%3A%2F%2Fjd.daqiso.com%2Fuploads%2Fallimg%2F150413%2F18-150413151J5.jpg";
//        JSONObject res = client.basicGeneralUrl(url, options);

        JSONObject res = client.basicGeneral(buf, options);
        Map<String, Object> map = JSONChange.json2map(res.toString());
        System.out.println(map.toString());
        return map;
    }

    //002.身份证识别
    @PostMapping("/idCard")
    public Map idCard(@RequestParam("file_idcard") MultipartFile[] fileArr) throws Exception {
        Map map = new HashMap<String,String>();
        MultipartFile mf1 = fileArr[0];
        MultipartFile mf2 = fileArr[1];
        byte[] by1 = mf1.getBytes();
        byte[] by2 = mf2.getBytes();
        AipOcr client = new AipOcr(baiduAiConfig.getAPP_ID(), baiduAiConfig.getAPI_KEY(), baiduAiConfig.getSECRET_KEY());
        JSONObject front = client.idcard(by1, "front", new HashMap<String, String>());
        JSONObject back = client.idcard(by2, "back", new HashMap<String, String>());
        map.put("front",JSONChange.json2map(front.toString()));
        map.put("back",JSONChange.json2map(back.toString()));
        return map;
    }

    //003.图像主体识别
    @PostMapping("/imageRecognition")
    public Map imageRecognition(@RequestParam("file_image") MultipartFile file) throws Exception{
        byte[] buf = file.getBytes();
        AipImageClassify client = new AipImageClassify(baiduAiConfig.getAPP_ID(), baiduAiConfig.getAPI_KEY(), baiduAiConfig.getSECRET_KEY());
        HashMap<String, String> options = new HashMap<>();
        options.put("baike_num","5");
        JSONObject res = client.advancedGeneral(buf, options);
        Map<String, Object> map = JSONChange.json2map(res.toString());
        return map;
    }

    //004.语音识别
    @PostMapping("/voiceRecognition")
    public Map voiceRecognition(@RequestParam("audioData") MultipartFile[] file) throws Exception{
        MultipartFile mf1 = file[0];
        byte[] buf = file[0].getBytes();
        AipSpeech client = new AipSpeech(baiduAiConfig.getAPP_ID(), baiduAiConfig.getAPI_KEY(), baiduAiConfig.getSECRET_KEY());
        JSONObject res = client.asr(buf, "wav", 16000, null);
        Map<String, Object> map = JSONChange.json2map(res.toString());
        return map;
    }

    //005.人脸识别登录
    @PostMapping("/faceLogin")
    public Map faceLogin(@RequestParam("snapData") String str) throws Exception{
        //使用Ajax提交base64字符串，需要经过去头转码
        String img_data = str.substring(22, str.length());
        AipFace client = new AipFace(baiduAiConfig.getAPP_ID(), baiduAiConfig.getAPI_KEY(), baiduAiConfig.getSECRET_KEY());
        HashMap<String, String> options = new HashMap<>();
        options.put("liveness_control","NORMAL");   //活体检测，级别低到高：LOW，NORMAL，HIGH
        JSONObject res = client.search(img_data, "BASE64", "DT_user_group_1", options);
        Map<String, Object> map = JSONChange.json2map(res.toString());
        return map;
    }
}
